﻿using Microsoft.Extensions.Configuration;

namespace ThresholdAssistant.ConfigSdk
{
    /// <summary>
    /// 配置工具类
    /// </summary>
    public class ConfigUtil
    {
        /// <summary>
        /// 获取Json配置
        /// </summary>
        /// <param name="key">配置文件的Key(格式：xxx:yyy，注意中间使用':'分割)</param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            return GetValue<string>(key);
        }

        /// <summary>
        /// 获取Json配置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">配置文件的Key(格式：xxx:yyy，注意中间使用':'分割)</param>
        /// <returns></returns>
        public static T GetValue<T>(string key)
        {
            return GetValue<T>(AppJson.FileDir, AppJson.FileName, key);
        }

        /// <summary>
        /// 获取Json配置
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="key">配置文件的Key(格式：xxx:yyy，注意中间使用':'分割)</param>
        /// <returns></returns>
        public static string GetValue(string fileName, string key)
        {
            return GetValue<string>(AppJson.FileDir, fileName, key);
        }

        /// <summary>
        /// 获取Json配置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="key">配置文件的Key(格式：xxx:yyy，注意中间使用':'分割)</param>
        /// <returns></returns>
        public static T GetValue<T>(string fileName, string key)
        {
            return GetValue<T>(AppJson.FileDir, fileName, key);
        }

        /// <summary>
        /// 获取Json配置
        /// </summary>
        /// <param name="fileDir">文件目录(格式：D:\JsonConfigs)</param>
        /// <param name="fileName">文件名称（格式：xxx.json）</param>
        /// <param name="key">配置文件的Key(格式：xxx:yyy，注意中间使用':'分割)</param>
        /// <returns></returns>
        public static string GetValue(string fileDir, string fileName, string key)
        {
            return GetValue<string>(fileDir, fileName, key);
        }

        /// <summary>
        /// 获取Json配置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileDir">文件目录(格式：D:\JsonConfigs)</param>
        /// <param name="fileName">文件名称（格式：xxx.json）</param>
        /// <param name="key">配置文件的Key(格式：xxx:yyy，注意中间使用':'分割)</param>
        /// <returns></returns>
        public static T GetValue<T>(string fileDir, string fileName, string key)
        {
            IConfiguration configuration = ComJsonConf.GetJsonConfiguration(fileDir, fileName);
            return configuration.GetSection(key).Get<T>();
        }

        /// <summary>
        /// 获取Json配置
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static T GetSettings<T>(string fileName)
        {
            IConfiguration configuration = GetJsonConfiguration(fileName);
            return ConfigurationBinder.Get<T>(configuration);
        }

        /// <summary>
        /// 获取Json配置
        /// </summary>
        /// <param name="fileDir"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static T GetSettings<T>(string fileDir, string fileName)
        {
            var configuration = ComJsonConf.GetJsonConfiguration(fileDir, fileName);
            return ConfigurationBinder.Get<T>(configuration);
        }

        /// <summary>
        /// 根据文件完整的路径获取Json配置
        /// </summary>
        /// <param name="fileName">文件名称（格式：xxx.json）</param>
        /// <returns></returns>
        public static IConfiguration GetJsonConfiguration(string fileName)
        {
            return ComJsonConf.GetJsonConfiguration(AppJson.FileDir, fileName);
        }
    }
}